package com.changgou.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changgou.framework.entity.PageResult;
import com.changgou.goods.dao.AlbumDao;
import com.changgou.goods.service.AlbumService;
import com.haha.goods.entity.Album;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author Yang
 * @date 2020/11/7
 */
@Service
public class AlbumServiceImpl extends ServiceImpl<AlbumDao, Album> implements AlbumService {

    @Override
    public PageResult findPage(Album album, int page, int size) {
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();

        if (!ObjectUtils.isEmpty(album)) {
            queryWrapper.eq(!StringUtils.isEmpty(album.getId()), "id", album.getId());
            queryWrapper.eq(!StringUtils.isEmpty(album.getImage()), "image", album.getImage());
            queryWrapper.eq(!StringUtils.isEmpty(album.getImageItems()), "image_items", album.getImageItems());
            queryWrapper.like(!StringUtils.isEmpty(album.getTitle()), "title", album.getTitle());
        }
        Integer num = baseMapper.selectCount(queryWrapper);
        int offset = (page-1)*size;
        List<Album> albums = baseMapper.findPage(queryWrapper,offset,size);
        PageResult pageResult = new PageResult();
        pageResult.setRows(albums);
        pageResult.setTotal(num);
        return pageResult;
    }
}
